var express = require('express');
var router = express.Router();
const jwt = require('jsonwebtoken');
const {secretOrPrivateKey} = require('../config')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.post('/login', function(req, res) {
  console.log('接收到前端的登录请求：', req.body);

  // 生成 token
  const token = jwt.sign({
    username: req.body.username
  }, secretOrPrivateKey, {
    expiresIn: '2h' // 过期时间，10s, 2h, 1d
  })

  setTimeout(() => {
    res.json({
      code: 200,
      msg: 'ok',
      token
    });
  }, 500)
});

module.exports = router;
